<template>
  <div class="semester_box" v-loading="loading">
    <div class="semester_addbtn">
      <el-button icon="plus" @click="openAddDialog()" type="primary">
        新增教师
      </el-button>
    </div>
    <el-table
      :data="teacherDatas?.list"
      border
      :header-row-style="{ color: '#232324', fontWeight: 'bold' }"
    >
      <el-table-column
        align="center"
        label="教师ID"
        min-width="120"
        prop="teacherId"
      />
      <el-table-column
        align="center"
        label="姓名"
        min-width="120"
        prop="name"
      />
      <el-table-column align="center" label="性别" min-width="80" prop="gender">
        <template #default="scope">
          <span v-if="scope.row.gender == 'male'">男</span>
          <span v-else>女</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="学院"
        min-width="160"
        prop="department"
      />
      <el-table-column
        align="center"
        label="职称"
        min-width="120"
        prop="title"
      />
      <el-table-column
        align="center"
        label="联系方式"
        min-width="160"
        prop="phone"
      />
      <el-table-column
        align="center"
        label="家庭住址"
        min-width="180"
        prop="address"
      />
      <el-table-column
        align="center"
        label="身份证号"
        min-width="180"
        prop="idCard"
      />
      <el-table-column
        align="center"
        label="出生日期"
        min-width="120"
        prop="birthday"
      />
      <el-table-column
        align="center"
        label="创建时间"
        min-width="180"
        prop="createTime"
      />

      <el-table-column align="center" fixed="right" label="操作" width="300">
        <template #default="scope">
          <el-button
            type="primary"
            link
            icon="edit"
            @click="openEditDialog(scope.row)"
          >
            编辑
          </el-button>
          <!-- 删除教师 -->
          <el-popconfirm
            :icon="InfoFilled"
            icon-color="#626AEF"
            width="180"
            title="是否删除该教师?"
            @confirm="deleteteacherById(scope.row.teacherId)"
          >
            <template #reference>
              <el-button type="primary" link icon="delete">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      style="margin-top: 12px"
      v-model:current-page="teacherPageParmas.pageNum"
      v-model:page-size="teacherPageParmas.pageSize"
      :page-sizes="[10, 20, 40]"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <el-dialog
      v-model="dialogFormVisible"
      :title="dialogTitle"
      style="min-width: 320px"
    >
      <el-form
        v-if="dialogFormVisible"
        ref="teacherForms"
        :inline="true"
        :model="teacherForm"
        :rules="rules"
        label-position="top"
        label-width="85px"
      >
        <el-form-item label="工号" prop="teacherId" style="width: 55%">
          <el-input
            v-model="teacherForm.teacherId"
            placeholder="请输入教师的工号"
          />
        </el-form-item>
        <el-form-item label="姓名" prop="name" style="width: 55%">
          <el-input v-model="teacherForm.name" placeholder="请输入教师的姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="gender" style="width: 55%">
          <el-radio-group v-model="teacherForm.gender">
            <el-radio label="male">男性</el-radio>
            <el-radio label="female">女性</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="出生日期" prop="birthday" style="width: 55%">
          <el-date-picker
            v-model="teacherForm.birthday"
            type="date"
            placeholder="请输入教师的出生日期"
          />
        </el-form-item>
        <el-form-item label="职称" prop="title" style="width: 55%">
          <el-input
            v-model="teacherForm.title"
            placeholder="请输入教师的职称"
          />
        </el-form-item>
        <el-form-item label="学院" prop="department" style="width: 55%">
          <el-input
            v-model="teacherForm.department"
            placeholder="请输入教师的学院"
          />
        </el-form-item>
        <el-form-item label="联系方式" prop="phone" style="width: 55%">
          <el-input
            v-model="teacherForm.phone"
            placeholder="请输入教师的联系方式"
          />
        </el-form-item>
        <el-form-item label="家庭住址" prop="address" style="width: 55%">
          <el-input
            v-model="teacherForm.address"
            placeholder="请输入教师的家庭住址"
          />
        </el-form-item>
        <el-form-item label="身份证号码" prop="idCard" style="width: 55%">
          <el-input
            v-model="teacherForm.idCard"
            placeholder="请输入教师的身份证号码"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="formConfirm(teacherForms)">
            提交
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { InfoFilled } from '@element-plus/icons-vue'
import type { FormInstance } from 'element-plus'
import {
  BasePageQuery,
  AdminUpadeTeacherParams,
  AdminAddTeacherParams,
  AdminGetTeacherRes,
} from '@/api/teacher/type'
import {
  adminAddTeacher,
  adminDeleteTeacher,
  adminGetTeacher,
  adminUpadeTeacher,
} from '@/api/teacher'

onMounted(async () => {
  await getteacherInfoPage()
})

const dialogFormVisible = ref<boolean>(false)

const teacherPageParmas: BasePageQuery = reactive({
  pageNum: 1,
  pageSize: 10,
})

const loading = ref(false)

const teacherDatas = ref<any>()

const total = ref(0)

const teacherForms = ref<FormInstance>()

let teacherForm: any = reactive({
  teacherId: 0,
  gender: '',
  birthday: '',
  department: '',
  title: '',
  phone: '',
  address: '',
  idCard: '',
})

const rules = reactive({
  teacherId: [{ required: true, message: '请输入教师工号', trigger: 'blur' }],
  name: [{ required: true, message: '请输入教师姓名', trigger: 'blur' }],
  gender: [{ required: true, message: '请输入教师性别', trigger: 'blur' }],
  birthday: [{ required: true, message: '请输入教师生日', trigger: 'blur' }],
  title: [{ required: true, message: '请输入教师职称', trigger: 'blur' }],
  department: [{ required: true, message: '请输入教师学院', trigger: 'blur' }],
  phone: [{ required: true, message: '请输入教师联系方式', trigger: 'blur' }],
  address: [{ required: true, message: '请输入教师家庭住址', trigger: 'blur' }],
  idCard: [{ required: true, message: '请输入教师身份证号', trigger: 'blur' }],
})

// 添加菜单方法，id为 0则为添加根菜单
const dialogTitle = ref('新增教师')
const isEdit = ref(false)

const getteacherInfoPage = async () => {
  loading.value = true
  try {
    const { data } = await adminGetTeacher(teacherPageParmas)
    teacherDatas.value = data
    total.value = data.total
  } catch (error) {
    if (error instanceof Error) {
      ElMessage.error(error.message)
    }
  } finally {
    loading.value = false
  }
}

const deleteteacherById = async (id: number) => {
  try {
    const { message } = await adminDeleteTeacher(id)
    ElMessage.success(message)
    await getteacherInfoPage()
  } catch (error) {
    ElMessage.error((error as Error).message)
  }
}

//添加信息
const addteacher = async () => {
  try {
    const { message } = await adminAddTeacher(teacherForm)
    ElMessage.success(message)
    teacherForm = {
      teacherId: 0,
      name: '',
      gender: '',
      birthday: '',
      department: '',
      title: '',
      phone: '',
      address: '',
      idCard: '',
    }
    await getteacherInfoPage()
  } catch (error) {
    ElMessage.error((error as Error).message)
  }
}

const updateteacher = async () => {
  try {
    const { message } = await adminUpadeTeacher(teacherForm)
    ElMessage.success(message)
    await getteacherInfoPage()
  } catch (error) {
    ElMessage.error((error as Error).message)
  }
}

//确认添加
const formConfirm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid) => {
    if (valid) {
      if (isEdit.value) {
        updateteacher()
      } else {
        addteacher()
      }
      dialogFormVisible.value = false
    }
  })
}

const openAddDialog = () => {
  isEdit.value = false
  if (teacherForm.teacherId != '0') {
    teacherForm = {
      teacherId: 0,
      name: '',
      gender: '',
      birthday: '',
      department: '',
      title: '',
      phone: '',
      address: '',
      idCard: '',
    }
  }
  dialogTitle.value = '新增教师'
  dialogFormVisible.value = true
}

// 修改菜单方法
const openEditDialog = async (data: any) => {
  isEdit.value = true
  dialogTitle.value = '编辑教师'
  teacherForm.teacherId = data.teacherId
  teacherForm.name = data.name
  teacherForm.gender = data.gender
  teacherForm.birthday = data.birthday
  teacherForm.department = data.department
  teacherForm.title = data.title
  teacherForm.phone = data.phone
  teacherForm.address = data.address
  teacherForm.idCard = data.idCard
  dialogFormVisible.value = true
}

const handleSizeChange = (val: number) => {
  getteacherInfoPage()
}
const handleCurrentChange = (val: number) => {
  getteacherInfoPage()
}
</script>
<style scoped lang="scss">
.semester_box {
  padding: 24px;

  .semester_addbtn {
    margin-bottom: 12px;
    display: flex;
  }
}
</style>
